package com.zyz.demo.controller;

import com.zyz.demo.model.BlogInfo;
import com.zyz.demo.model.Result;
import com.zyz.demo.service.BlogService;
import com.zyz.demo.utils.JwtUtils;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    private String[] markdownChar = new String[]{"#","~","`","*","-"};


    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        List<BlogInfo> blogInfoList = blogService.getList();
        // 在list页面修改博客缩略展示(去除所有的‘#’  只展示最多30个字)
        for (BlogInfo blogInfo : blogInfoList) {
            String content = blogInfo.getContent();
            if (content.length() > 30) {
                content = content.substring(0,30);
            }
            for (String markdown : markdownChar) {
                content = content.replace(markdown,"");
            }
            blogInfo.setContent(content);
        }
        if (blogInfoList == null) {
            log.warn("无博客");
        }
        return blogInfoList;
    }

    @RequestMapping("/getBlogById")
    public Result getBlogById(Integer blogId,HttpServletRequest request) {
        log.info("接收id参数："+blogId);
        if (blogId < 0) {
            log.warn("id为负数");
            return Result.fail("传入的id为负数");
        }
        BlogInfo blogInfo = blogService.getBlogById(blogId);
        Integer userId = blogInfo.getUserId();
        // 通过token拿到登录用户Id
        String userToken = request.getHeader("user_token");
        Integer userIdByToken = JwtUtils.getUserIdByToken(userToken);
        if (userId.intValue() == userIdByToken.intValue()) blogInfo.setIsLoginUser(true);
        else blogInfo.setIsLoginUser(false);

        if (blogInfo == null) {
            log.warn("文章不存在");
            return Result.fail("文章不存在");
        }
        return Result.success(blogInfo);
    }

    @RequestMapping("/addBlog")
    public Boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
//        log.info(String.valueOf("blog.addBlog blogInfo:" + blogInfo.getTitle()==null));
        String userToken = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdByToken(userToken);
        blogInfo.setUserId(userId);
        // 校验
        Assert.hasLength(blogInfo.getTitle(),"title不能为空");
        Assert.hasLength(blogInfo.getContent(),"content不能为空");
        Integer result = null;
        try{
            result = blogService.addBlog(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch(Exception e) {
            log.error("博客添加失败 e："+e.getMessage());
        }
        return false;
    }

    @RequestMapping("/update")
    public Boolean updateBlog(@RequestBody BlogInfo blogInfo) {
        Assert.notNull(blogInfo.getId(),"blogId为空");
        Assert.hasLength(blogInfo.getTitle(),"title为空");
        Assert.hasLength(blogInfo.getContent(),"content为空");
        Integer result = null;
        try{
            result = blogService.updateBlog(blogInfo);
            if (result == 1) {
                log.info("blog更新成功！");
                return true;
            }
        } catch(Exception e) {
            log.error("blog更新失败！");
        }
        return false;
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId) {
        Assert.notNull(blogId,"blogId为空");
        Integer result = null;
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        try{
            result = blogService.updateBlog(blogInfo);
            if (result == 1) {
                log.info("blog删除成功！");
                return true;
            }
        } catch(Exception e) {
            log.error("blog删除失败！");
        }
        return false;
    }

    // 写博客拦截接口
    @RequestMapping("/deitInterceptor")
    public void deitInterceptor(){}

}
